%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               replication_PRD.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        Generates best responses data for Figure 6 
%
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
path='C:/Users/mrueda/Documents/MATLAB/Party_delegates/R&R/replication/';

%Loading baseline game LL
load(strcat(path,'2playerc.mat'));
beta2=beta;
Llong2=Llong;
LL2=LL;
auxLL2=auxLL;
clearvars -except LL2 Llong2 auxL2L beta2 path

%Importing data
data=strcat(path,'panel_matlab_r&r.xls');
[data2012,~]=xlsread(data,'2012');
[data2009,labels]=xlsread(data,'2009');
data=[data2009;data2012];

d_s=dataset({data(:,:) labels{:}});

%Extended
varsx={'l_d_RC_others2' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'l_diff_PRI_PRD' 'l_diff_PAN_PRD' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
'l_d_RC_others2_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'l_diff_PRI_PRD_sq' 'l_diff_PAN_PRD_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
'l_d_RC_others2_l_margin' 'l_d_RC_others2_lcasillas' 'l_d_RC_others2_school_ic' 'l_d_RC_others2_l_diff_PRI_PAN' 'l_d_RC_others2_l_diff_PRI_PRD' 'l_d_RC_others2_l_diff_PAN_PRD' 'l_d_RC_others2_lpop' 'l_d_RC_others2_l_turnout' 'l_d_RC_others2_local' 'l_d_RC_others2_ldist_city' ...
'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_l_diff_PRI_PRD' 'l_margin_l_diff_PAN_PRD' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' ...
'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_l_diff_PRI_PRD' 'lcasillas_l_diff_PAN_PRD' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
'school_ic_l_diff_PRI_PAN' 'school_ic_l_diff_PRI_PRD' 'school_ic_l_diff_PAN_PRD' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' 'l_diff_PRI_PRD_lpop' 'l_diff_PRI_PRD_l_turnout' 'l_diff_PRI_PRD_local' 'l_diff_PRI_PRD_ldist_city' ...
'l_diff_PAN_PRD_lpop' 'l_diff_PAN_PRD_l_turnout' 'l_diff_PAN_PRD_local' 'l_diff_PAN_PRD_ldist_city' ...
'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
'l_turnout_local' 'l_turnout_ldist_city' ...
'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN' 'ldist_prd' 'gov_PRD'};

varsy={'d_RC_PRI_c' 'd_RC_PAN_c' 'd_RC_PRD_c'};
varcl={'state_district'};
varcl2={'id_state'};
[Y,X,CLv]=set_design_mat(d_s,varsy,varsx,[varcl varcl2]);

%States where there is enough variation in dep. var to estimate first stage separately for each party
    states_PRI=[9 11 12 14 17 19 20 22 24 26]';
    states_PAN=[5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 30]';
    states_PRD=[5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 30]';

    CLv_PRI=CLv(:,2);
    CLv_PRI(ismember(CLv_PRI(:,1),setdiff(CLv_PRI(:,1),states_PRI)),1)=1;
    CLv_PAN=CLv(:,2);
    CLv_PAN(ismember(CLv_PAN(:,1),setdiff(CLv_PAN(:,1),states_PAN)),1)=1;
    CLv_PRD=CLv(:,2);
    CLv_PRD(ismember(CLv_PRD(:,1),setdiff(CLv_PRD(:,1),states_PRD)),1)=1;
%CLv_PRI and CL_vPAN have state codes for those states where the first stage can be estimated (for each party) and a 1 for all others 

%Remember: when runnning main function without state by state fist stage,
%still include state vectors CLv_PAN and CLv_PRI, for each party


[beta,hessian,phats,LL3,auxLL3,Llong3]=representatives_game2(Y,X,0,CLv_PAN,CLv_PRI,CLv_PRD);

%Computing BICs
N=size(Llong2,1);
BIC2=2*LL2+log(N)*size(beta2(:),1)
BIC3=2*LL3+log(N)*size(beta(:),1)

%Computing Vuong Stat
w=sqrt(1/N*(sum(Llong2.^2)-2*sum(Llong2.*Llong3)+sum(Llong3.^2))-1/(N^2)*(sum(Llong2)^2-2*sum(Llong2)*sum(Llong3)+sum(Llong3)^2));
V=1/(sqrt(N))*(sum(Llong2)-sum(Llong3))/w

nReps=500;
[betas_monte_extended,se,pvals,~,~]=bootstrap_game2(Y,X,beta,nReps,0,CLv_PAN,CLv_PRI,CLv_PRD,CLv(:,1));
ses=reshape(se,[length(se)/6,6]);
pvals=reshape(pvals,[length(pvals)/6,6]);
labels={'l_d_RC_others2' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'l_diff_PRI_PRD' 'l_diff_PAN_PRD' 'lpop' 'l_turnout' 'local' 'ldist_city'};
results=table_game2(beta,ses,labels)

%Graph PAN's and PRI's best responses to changes in prob high of PRD
var=16; range_var=(0.2:0.05:0.8)';
pri_pan_prd=3;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=1;
%High PRI
[UCIhpri_prdh,LCIhpri_prdh,predhpri_prdh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRI
[UCImpri_prdh,LCImpri_prdh,predmpri_prdh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
%High PAN
[UCIhpan_prdh,LCIhpan_prdh,predhpan_prdh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PAN
[UCImpan_prdh,LCImpan_prdh,predmpan_prdh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

%Graph PAN's and PRI's best responses to changes in prob med of PRD
var=17; range_var=(0.1:0.025:0.4)';
pri_pan_prd=3;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=1;
%High PRI
[UCIhpri_prdm,LCIhpri_prdm,predhpri_prdm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRI
[UCImpri_prdm,LCImpri_prdm,predmpri_prdm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
%High PAN
[UCIhpan_prdm,LCIhpan_prdm,predhpan_prdm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PAN
[UCImpan_prdm,LCImpan_prdm,predmpan_prdm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

%Graph PRD's and PAN's best responses to changes in prob high of PRI
var=14; range_var=(0.2:0.05:0.8)';
pri_pan_prd=1;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=3;
%High PRD
[UCIhprd_prih,LCIhprd_prih,predhprd_prih]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRD
[UCImprd_prih,LCImprd_prih,predmprd_prih]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
%High PAN
[UCIhpan_prih,LCIhpan_prih,predhpan_prih]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PAN
[UCImpan_prih,LCImpan_prih,predmpan_prih]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

%Graph PAN's and PRD's best responses to changes in prob med of PRI
var=15; range_var=(0.1:0.025:0.4)';
pri_pan_prd=1;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=3;
%High PRD
[UCIhprd_prim,LCIhprd_prim,predhprd_prim]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRD
[UCImprd_prim,LCImprd_prim,predmprd_prim]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
%High PAN
[UCIhpan_prim,LCIhpan_prim,predhpan_prim]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PAN
[UCImpan_prim,LCImpan_prim,predmpan_prim]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

%Graph PRI's and PRD's best responses to changes in prob high of PAN
var=14; range_var=(0.2:0.05:0.8)';
pri_pan_prd=2;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=1;
%High PRI
[UCIhpri_panh,LCIhpri_panh,predhpri_panh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRI
[UCImpri_panh,LCImpri_panh,predmpri_panh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
var=16; range_var=(0.2:0.05:0.8)';
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=3;
%High PRD
[UCIhprd_panh,LCIhprd_panh,predhprd_panh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRD
[UCImprd_panh,LCImprd_panh,predmprd_panh]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);


%Graph PRI's and PRD's best responses to changes in prob mid of PAN
var=15; range_var=(0.1:0.025:0.4)';
pri_pan_prd=2;
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=1;
%High PRI
[UCIhpri_panm,LCIhpri_panm,predhpri_panm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRI
[UCImpri_panm,LCImpri_panm,predmpri_panm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

pri_pan_prd=2;
var=17; range_var=(0.2:0.05:0.8)';
[mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats);

pri_pan_prd=3;
%High PRD
[UCIhprd_panm,LCIhprd_panm,predhprd_panm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,0);
%Medium PRD
[UCImprd_panm,LCImprd_panm,predmprd_panm]=marginal2(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,mean_X_PRD,pri_pan_prd,1);

graph=[range_var UCIhpri_prdm   LCIhpri_prdm   predhpri_prdm   UCIhpan_prdm    LCIhpan_prdm    predhpan_prdm UCIhpan_prim  LCIhpan_prim    predhpan_prim   UCIhprd_prim    LCIhprd_prim    predhprd_prim UCIhprd_panm  LCIhprd_panm    predhprd_panm   UCIhpri_panm    LCIhpri_panm    predhpri_panm   UCIhpri_prdh    LCIhpri_prdh predhpri_prdh  UCIhpan_prdh    LCIhpan_prdh    predhpan_prdh UCIhpan_prih  LCIhpan_prih    predhpan_prih   UCIhprd_prih    LCIhprd_prih    predhprd_prih   UCIhprd_panh LCIhprd_panh   predhprd_panh   UCIhpri_panh    LCIhpri_panh    predhpri_panh UCImpri_prdm  LCImpri_prdm    predmpri_prdm   UCImpan_prdm    LCImpan_prdm    predmpan_prdm   UCImpri_prdh    LCImpri_prdh predmpri_prdh  UCImpan_prdh    LCImpan_prdh    predmpan_prdh   UCImprd_prim    LCImprd_prim predmprd_prim  UCImpan_prim    LCImpan_prim    predmpan_prim UCImprd_prih  LCImprd_prih    predmprd_prih   UCImpan_prih    LCImpan_prih    predmpan_prih   UCImprd_panm    LCImprd_panm    predmprd_panm UCImpri_panm  LCImpri_panm   predmpri_panm UCImprd_panh LCImprd_panh predmprd_panh UCImpri_panh LCImpri_panh predmpri_panh];
labels_graph={'range_var' 'UCIhpri_prdm' 'LCIhpri_prdm' 'predhpri_prdm' 'UCIhpan_prdm' 'LCIhpan_prdm' 'predhpan_prdm' 'UCIhpan_prim' 'LCIhpan_prim' 'predhpan_prim' 'UCIhprd_prim' 'LCIhprd_prim' 'predhprd_prim' 'UCIhprd_panm' 'LCIhprd_panm' 'predhprd_panm' 'UCIhpri_panm' 'LCIhpri_panm' 'predhpri_panm' 'UCIhpri_prdh' 'LCIhpri_prdh' 'predhpri_prdh' 'UCIhpan_prdh' 'LCIhpan_prdh' 'predhpan_prdh' 'UCIhpan_prih' 'LCIhpan_prih' 'predhpan_prih' 'UCIhprd_prih' 'LCIhprd_prih' 'predhprd_prih' 'UCIhprd_panh' 'LCIhprd_panh' 'predhprd_panh' 'UCIhpri_panh' 'LCIhpri_panh' 'predhpri_panh' 'UCImpri_prdm' 'LCImpri_prdm' 'predmpri_prdm' 'UCImpan_prdm' 'LCImpan_prdm' 'predmpan_prdm' 'UCImpri_prdh' 'LCImpri_prdh' 'predmpri_prdh' 'UCImpan_prdh' 'LCImpan_prdh' 'predmpan_prdh' 'UCImprd_prim' 'LCImprd_prim' 'predmprd_prim' 'UCImpan_prim' 'LCImpan_prim' 'predmpan_prim' 'UCImprd_prih' 'LCImprd_prih' 'predmprd_prih' 'UCImpan_prih' 'LCImpan_prih' 'predmpan_prih' 'UCImprd_panm' 'LCImprd_panm' 'predmprd_panm' 'UCImpri_panm' 'LCImpri_panm' 'predmpri_panm' 'UCImprd_panh' 'LCImprd_panh' 'predmprd_panh' 'UCImpri_panh' 'LCImpri_panh' 'predmpri_panh'};
graph_br=dataset({graph(:,:) labels_graph{:}});
  
export(graph_br,'XLSfile',strcat(path,'pred_best_response_r&rc.xls'))





